var _baseEvent = function(){
	"use strict";
	this._eventPool = {};
	
	this.setEvent = function(eventName,event){
		return this._eventPool[eventName] = event;
	}
	
	this.runEvent = function(eventName){
		if(this.hasEvent(eventName)){
			var eventFun = this._eventPool[eventName];
			var _arguments = [];
			if(typeof arguments[1] != 'undefined' && arguments[1].constructor == Array){
				for(var argumentId in arguments[1]){
					if(arguments[1][argumentId].constructor == String){
						_arguments.push('"'+arguments[1][argumentId]+'"');
					}else{
						_arguments.push(arguments[1][argumentId]);
					}
					
				}
				_arguments = _arguments.join(',');
			}
			var runCommand = 'eventFun('+_arguments+')';
			return eval(runCommand);
		}
		return undefined;
	}
	
	this.hasEvent = function(eventName){
		return this._eventPool.hasOwnProperty(eventName);
	}
	
}

var _baseComponent = function(){
	"use strict";
	_baseEvent.apply(this);
	this.component = function(name,component){
		this[name] = new component;
	}
}


var auctionData = function(){
	"use strict";
	this.title = 0;
	this.winner_name = '';
	this.winner_price = 0;
	this.last_time = 0;
	this.slef = {};
	this.id = 0;
	this.attributes = function(attributes){
		for(var attributeName in attributes){
			if(this.hasOwnProperty(attributeName)){
				this[attributeName] = attributes[attributeName];
				this.runEvent('setAttribute.after',[attributeName,attributes[attributeName]]);
			}
		}
	}
}

var auctionPoolClient = function(){
	"use strict";	
	this._slef = {}
	this._auctionPool = {}
	
	this.addSlef = function(slef,auctionSlef){
		this._slef[slef] = auctionSlef;
	}
	
	this.getSlef = function(slef){
		return this._slef[slef];
	}
	
	this.slefs = function(){
		return this._slef;
	}

	this.addAuction = function(auctionData){
		this._auctionPool[auctionData.id] = auctionData; 
	}
	
	this.getAuction = function(id){
		this._auctionPool[id];
	}
	
	this.auctions = function(){
		return this._auctionPool;
	}
	
}
var _client = function(){
	this._timer = false;
	_baseComponent.apply(this);
	
	this.interval = function(){
		
	}
	
	this.run = function(){
		if(this._timer === false){
			this._timer = setInterval(this.interval,1000);
		}
	}
}

client = (function($){
	"use strict";
	var _obj = false;
	if (_obj === false){
		_obj = new _client();
	}
	return _obj;
})(jQuery);


var auctionSlef = function(){
	"use strict";
	_baseComponent.apply(this);
	this.name = '';
	this.setEvent('itemPubOn.validate',function(){
		return true;
	})
	
	this.slefConfig = {}
	this.setAttribute = function(attribute,attributeFun){
		if(!this.slefConfig.hasOwnProperty(attribute)){
			this.slefConfig[attribute]={
					'setAttribute':function(){}
			} 
		}
		this.slefConfig[attribute]['setAttribute'] = attributeFun;
	}
		
	this.getAttribute = function(attribute,attributeFun){
		this.slefConfig[attribute]['getValue'] = attributeFun;
	}
	
	this.itemPubOn = function(auctionData){
		if(this.runEvent('itemPubOn.validate')){
			auctionData.pubOnSlef(this.name);
			var _this = this;
			auctionData.setEvent('setAttribute.after',function(attribute,attributeValue){
				_this.slefConfig[attribute]['setAttribute'](attribute,attributeValue);
			});
		}
	}
	
	this.itemPutOff = function(){
		
	}
	
	this.itemUpdate = function(){
		
	}
	
	this.config = function(config){
		if(config.hasOwnProperty('setAttribute')){
			for(var attribute in config['setAttribute']){
				this.setAttribute(attribute,config['setAttribute'][attribute]);
			}			
		}
	}
}

client.component('slefPool',function(){
	"use strict";
	this.slefPool = {}
	this.setSlef = function(){
		
	}
});

var auctionData = function(){
	"use strict";
	_baseComponent.apply(this);
	this.title = 0;
	this.winner_name = '';
	this.winner_price = 0;
	this.last_time = 0;
	this.slef = {};
	this.id = 0;
	this.category_id = 0;
	
	this.attributes = function(attributes){
		for(var attributeName in attributes){
			if(this.hasOwnProperty(attributeName)){
				this[attributeName] = attributes[attributeName];
				this.runEvent('setAttribute.after',[attributeName,attributes[attributeName]]);
			}
		}
	}
	
	this.pubOnSlef = function(slef){
		this.slef[slef] = slef;
	}
}

var auctionData = function(){
	"use strict";
	_baseComponent.apply(this);
	this.slef = {}
	this.attributeMapper = {
		'title':'',
		'winner_name':'',
		'winner_price':0,
		'last_time':0,
		'id':0,
		'category_id':0
	}
	
	this.setAttribute = function(attribute,value){
		if(this.attributeMapper.hasOwnProperty(attribute)){
			this.attributeMapper[attribute] = value;
			this.runEvent('setAttribute.after',[attribute,this.attributeMapper[attribute]]);
		}
	}
	
	this.getAttribute = function(attribute){
		return this.attributeMapper[attribute];
	}
}

var auctionSlef = function(){
	"use strict";
	_baseComponent.apply(this);
	this.name = '';
	this.setEvent('itemPubOn.validate',function(){
		return true;
	})
	
	this.itemPubOn = function(auctionData){
		if(this.runEvent('itemPubOn.validate')){
			auctionData.pubOnSlef(this.name);
			var _this = this;
			auctionData.setEvent('setAttribute.after',function(attribute,attributeValue){
				_this.slefConfig[attribute]['setAttribute'](attribute,attributeValue);
			});
		}
	}
	
	this.itemPutOff = function(){
		
	}
	
	this.itemUpdate = function(){
		
	}
	
	this.config = function(config){
		if(config.hasOwnProperty('setAttribute')){
			for(var attribute in config['setAttribute']){
				this.setAttribute(attribute,config['setAttribute'][attribute]);
			}			
		}
	}
}


